package com.pangus.ims.streamax.service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aspose.slides.p2cbca448.ifd;
import com.pangus.common.sys.service.ConfigService;
import com.pangus.common.sys.utils.SqlQueryUtils;
import com.pangus.fa.mybatis.mapper.entity.Condition;
import com.pangus.fa.mybatis.mapper.entity.Condition.Criteria;
import com.pangus.fa.util.StringUtils;
//import com.pangus.ims.agedevbefore.service.AgeDevBeforServer;
import com.pangus.ims.bs.BsConstants;
import com.pangus.ims.mes.dto.domain.MesAgeDevBindInfoDto;
import com.pangus.ims.mes.dto.domain.MesAgeDevInfoDto;
import com.pangus.ims.mes.dto.domain.MesAgeDevInfoHighDto;
import com.pangus.ims.mes.form.MesAgeDevBindInfoForm;
import com.pangus.ims.mes.service.MesAgeDevBindInfoService;
import com.pangus.ims.streamax.dto.domain.MesAgeDevLogInfoDto;
import com.pangus.ims.streamax.dto.domain.MesAgeDevNumber;
import com.pangus.ims.streamax.dto.domain.ResultMap;
import com.pangus.ims.streamax.form.MesAgeDevInfoForm;
import com.pangus.ims.streamax.util.N95Utils;
import com.pangus.ims.streamax.util.StreamaxConstants;
import com.pangus.ims.streamax.util.StreamaxUtils;

@Service
public class MesAgeDevAllService {

	private static final Logger logger = LoggerFactory.getLogger(MesAgeDevAllService.class.getName());
	
	@Autowired
	private SqlQueryUtils sqlQueryUtils;

	@Autowired
	private MesAgeDevInfoService mesAgeDevInfoService;

	@Autowired
	private MesAgeDevBindInfoService mesAgeDevBindInfoService;

	@Autowired
	private ConfigService configService;
	
	//logo上传
//	boolean logoError;
	//视频验证
//	boolean videoError;

	/**
	 *  查询货架上所有的产品信息
	 * @param params
	 * @return
	 */
	public Map<String, Object> listAgeDevBindInfoAll(MesAgeDevBindInfoForm params) {
		String ipAddr = N95Utils.getIpAddr();
		if (params.getCurr() == null)
			params.setCurr(1);
		if (params.getPageSize() == null)
			params.setPageSize(9999);
		Map<String, Object> dataAll = new HashMap<>();
		logger.info("当前ip:"+ipAddr);
		List<MesAgeDevBindInfoForm> listsAgeDevBindInfoForms = new ArrayList<MesAgeDevBindInfoForm>();
		Integer start  = 1;
		Integer end = 100;
		if(params.getMinCode()!=null) {
			start = params.getMinCode();
		}
		
		if(params.getMaxCode()!=null) {
			end = params.getMaxCode();
		}
		
		//String sql = "select * from MES_AGE_DEV_BIND_INFO where 1=1 AND DEV_TYPE = 'Y' order by box_code asc,STOCK_CODE ASC ";
		String sql ="SELECT  "+
				"	AGE_DEV_BIND_INFO_ID , "+
				"	ORG_ID , "+
				"	STOCK_CODE, "+
				"	TO_NUMBER(scode) , "+
				"	LB_ID , "+
				"	NEW_LB_ID , "+
				"	BIND_DATE , "+
				"	IS_VALID , "+
				"	MO_ID , "+
				"	SIGNAL_STAT , "+
				"	BOX_CODE , "+
				"	UPLOAD_TYPE, "+
				"	CREATED_BY , "+
				"	CREATED_DATE  "+
				"FROM  ( "+
				"	SELECT "+
				"		AGE_DEV_BIND_INFO_ID , "+
				" TO_NUMBER(SUBSTR(STOCK_CODE,0,2)) AS SEQ1,"+
				" TO_NUMBER(SUBSTR(STOCK_CODE,4,2)) AS SEQ2,"+
				" TO_NUMBER(SUBSTR(STOCK_CODE,7,3)) AS SEQ3,"+
				"		ORG_ID , "+
				"		STOCK_CODE, "+
				"		SUBSTR(STOCK_CODE,0,2) AS scode  , "+
				"		LB_ID , "+
				"		NEW_LB_ID , "+
				"		BIND_DATE , "+
				"		IS_VALID , "+
				"		MO_ID , "+
				"		SIGNAL_STAT , "+
				"		BOX_CODE , "+
				"		UPLOAD_TYPE, "+
				"		CREATED_BY , "+
				"		CREATED_DATE  "+
				"	FROM "+
				"		MES_AGE_DEV_BIND_INFO madbi "+
				"	WHERE "+
				"		DEV_TYPE = 'N' "+
				"	ORDER BY "+
				"		SEQ1,SEQ2,SEQ3 ASC "+
				")	 "+
				"WHERE scode  BETWEEN  "+start+"  AND "+end+" ";
		List<MesAgeDevBindInfoForm> devBindInfos = sqlQueryUtils.queryByParamPages(sql, null, params.getCurr(),params.getPageSize(), MesAgeDevBindInfoForm.class);
		if (devBindInfos != null && devBindInfos.size() > 0) {
			logger.info("货架总个数:"+devBindInfos.size());
			LocalDateTime allStartTime = LocalDateTime.now();
			MesAgeDevNumber number = new MesAgeDevNumber();
			for (MesAgeDevBindInfoForm bindInfo : devBindInfos) {
				MesAgeDevBindInfoForm mesAgeDevBindInfoForm = new MesAgeDevBindInfoForm();
				BeanUtils.copyProperties(bindInfo, mesAgeDevBindInfoForm);
				if (StringUtils.isBlank(bindInfo.getMoId())) {
					number.setDef(number.getDef()+1);//闲置新增1
					mesAgeDevBindInfoForm.setShowType("def");
				} else {
					// 根据标签id和工单Id查找设备信息
					String sqlAgeDev = " SELECT ad.*  FROM  mes_age_dev_info ad "
							+ "WHERE ad.mo_id = ? and ad.lb_id=? and ad.valid_flag='1'";
					LocalDateTime startTime = LocalDateTime.now();
					List<MesAgeDevInfoForm> ageDevInfo = sqlQueryUtils.queryAll(sqlAgeDev,
							new Object[] { bindInfo.getMoId(), bindInfo.getLbId() }, MesAgeDevInfoForm.class);
					StreamaxUtils.caclulateTime(startTime, "查询货架当个设备信息", logger);
					if (ageDevInfo != null && ageDevInfo.size() > 0) {
						MesAgeDevInfoForm ageDevInfoOne= ageDevInfo.get(0);
						//获取参数配置信息
						ageDevInfoOne.setFtpPath1(ageDevInfoOne.getFtpPath1());
						ageDevInfoOne.setFtpSoft1(ageDevInfoOne.getFtpSoft1());
						ageDevInfoOne.setAgeTime(ageDevInfoOne.getAgeTime());
						ageDevInfoOne.setPlatType(ageDevInfoOne.getPlatType());
						//1.客供平台:a.是否过站。b.是否连接。c.是否异常。
						if(ageDevInfoOne.getPlatType()==null)ageDevInfoOne.setPlatType(1);
						//1.平台版本:a.是否过站。c.是否异常。
						if (2 == ageDevInfoOne.getPlatType()) {//客供
							long min =  0;
							if(ageDevInfoOne.getDisConnTime()!=null) {
								min = N95Utils.calcTime(ageDevInfoOne.getDisConnTime(), new Date(), "m");
							}
							if (min >15){
								ageDevInfoOne.setIsOver(1);
							}
							countNumberByType( ageDevInfoOne, mesAgeDevBindInfoForm, number, 2);
						}else {
							countNumberByType( ageDevInfoOne, mesAgeDevBindInfoForm, number, 1);
						}
						//判断上传和视频验证
						countNumberByType( ageDevInfoOne, mesAgeDevBindInfoForm, number, 9);
					} else {// 未识别到设备信息时，等待时长为当前时间-绑定货架时间
						logger.info("无设备信息"+bindInfo.getBindDate());
						if (bindInfo.getBindDate() != null) {
							mesAgeDevBindInfoForm.setWaitMin(String.valueOf(N95Utils.calcTime(bindInfo.getBindDate(), new Date(), "m")));
						}
						number.setUnconn(number.getUnconn()+1);
						mesAgeDevBindInfoForm.setShowType("unconn");
					}
				}
				listsAgeDevBindInfoForms.add(mesAgeDevBindInfoForm);
			}
			StreamaxUtils.caclulateTime(allStartTime, "查询完所有货架产品信息耗时", logger);
			// 统计信息
			dataAll.put("data", listsAgeDevBindInfoForms);
			//设置头部数量统计单页
			dataAll.put("headInfo", number);
			//设置头部数量统计
			dataAll.put("headInfoAll", listAllNumber(start,end));
			//设置系统参数
			setSystemInfo(dataAll);
		}else {
			MesAgeDevNumber number = new MesAgeDevNumber();
			dataAll.put("headInfoAll", number);
			dataAll.put("headInfo", number);
		}
		return dataAll;
	}

	
	
	/**
	 *  查询货架上所有的产品信息
	 * @param params
	 * @return
	 */
	public Map<String, Object> listAgeDevBindInfoAllHigh(MesAgeDevBindInfoForm params) {
		String ipAddr = N95Utils.getIpAddr();
		if (params.getCurr() == null)
			params.setCurr(1);
		if (params.getPageSize() == null)
			params.setPageSize(9999);
		Map<String, Object> dataAll = new HashMap<>();
		logger.info("当前ip:"+ipAddr);
		List<MesAgeDevBindInfoForm> listsAgeDevBindInfoForms = new ArrayList<MesAgeDevBindInfoForm>();
		String sql = "select * from MES_AGE_DEV_BIND_INFO where 1=1 AND DEV_TYPE = 'Y' ";
				if(params.getDataType()==1) {
					sql += " and api_ip = '10.28.6.5' ";
				}else {
					sql += " and api_ip = '10.28.10.210' ";
				}
				//根据类型显示不同货架数据。
				sql+= "order by box_code asc,STOCK_CODE ASC ";
		List<MesAgeDevBindInfoForm> devBindInfos = sqlQueryUtils.queryByParamPages(sql, null, params.getCurr(),params.getPageSize(), MesAgeDevBindInfoForm.class);
		if (devBindInfos != null && devBindInfos.size() > 0) {
			logger.info("货架总个数:"+devBindInfos.size());
			LocalDateTime allStartTime = LocalDateTime.now();
			MesAgeDevNumber number = new MesAgeDevNumber();
			for (MesAgeDevBindInfoForm bindInfo : devBindInfos) {
				MesAgeDevBindInfoForm mesAgeDevBindInfoForm = new MesAgeDevBindInfoForm();
				BeanUtils.copyProperties(bindInfo, mesAgeDevBindInfoForm);
				if (StringUtils.isBlank(bindInfo.getMoId())) {
					number.setDef(number.getDef()+1);//闲置新增1
					mesAgeDevBindInfoForm.setShowType("def");
				} else {
					// 根据标签id和工单Id查找设备信息
					String sqlAgeDev = " SELECT ad.*  FROM  mes_age_dev_info ad "
							+ "WHERE ad.mo_id = ? and ad.lb_id=? and ad.valid_flag='1'";
					LocalDateTime startTime = LocalDateTime.now();
					List<MesAgeDevInfoForm> ageDevInfo = sqlQueryUtils.queryAll(sqlAgeDev,
							new Object[] { bindInfo.getMoId(), bindInfo.getLbId() }, MesAgeDevInfoForm.class);
					StreamaxUtils.caclulateTime(startTime, "查询货架当个设备信息", logger);
					if (ageDevInfo != null && ageDevInfo.size() > 0) {
						MesAgeDevInfoForm ageDevInfoOne= ageDevInfo.get(0);
						//获取参数配置信息
						ageDevInfoOne.setFtpPath1(ageDevInfoOne.getFtpPath1());
						ageDevInfoOne.setFtpSoft1(ageDevInfoOne.getFtpSoft1());
						ageDevInfoOne.setAgeTime(ageDevInfoOne.getAgeTime());
						ageDevInfoOne.setPlatType(ageDevInfoOne.getPlatType());
						//1.客供平台:a.是否过站。b.是否连接。c.是否异常。
						
						if(ageDevInfoOne.getPlatType()==null)ageDevInfoOne.setPlatType(1);
						//1.平台版本:a.是否过站。c.是否异常。
						if (2 == ageDevInfoOne.getPlatType()) {//客供
							long min = N95Utils.calcTime(ageDevInfoOne.getDisConnTime(), new Date(), "m");
							if (min >15){
								ageDevInfoOne.setIsOver(1);
							}
							countNumberByType( ageDevInfoOne, mesAgeDevBindInfoForm, number, 2);
						}else {
							countNumberByType( ageDevInfoOne, mesAgeDevBindInfoForm, number, 1);
						}
					} else {// 未识别到设备信息时，等待时长为当前时间-绑定货架时间
						if (bindInfo.getBindDate() != null) {
							mesAgeDevBindInfoForm.setWaitMin(String.valueOf(N95Utils.calcTime(bindInfo.getBindDate(), new Date(), "m")));
						}
						number.setUnconn(number.getUnconn()+1);
						mesAgeDevBindInfoForm.setShowType("unconn");
					}
				}
				//U盘升级的，计算老化时长和剩余时间
				if(mesAgeDevBindInfoForm.getLbId()!=null && mesAgeDevBindInfoForm.getUploadType()!=null && mesAgeDevBindInfoForm.getUploadType()==2) {
					String sqlPanel = "	SELECT created_date,age_time FROM MES_AGE_DEV_INFO_HIGH WHERE lb_id=? AND AGE_TYPE =1  ORDER BY CREATED_DATE  DESC ";
					List<MesAgeDevInfoHighDto> bindInfos = sqlQueryUtils.queryAll(sqlPanel, new Object[] {mesAgeDevBindInfoForm.getLbId()},MesAgeDevInfoHighDto.class);
					if(bindInfos!=null && bindInfos.size()>0) {
						MesAgeDevInfoHighDto dto = bindInfos.get(0);
						long min = dto.getAgeTime().longValue();
						mesAgeDevBindInfoForm.setAgeTime(min);
						long passMin = N95Utils.getDatemin(new Date(), dto.getCreatedDate());
						long diff = min-passMin;
						mesAgeDevBindInfoForm.setWaitMin(String.valueOf(diff));
					}
					
				}
				listsAgeDevBindInfoForms.add(mesAgeDevBindInfoForm);
			}
			StreamaxUtils.caclulateTime(allStartTime, "查询完所有货架产品信息耗时", logger);
			// 统计信息
			dataAll.put("data", listsAgeDevBindInfoForms);
			//设置头部数量统计单页
			dataAll.put("headInfo", number);
			//设置头部数量统计
			dataAll.put("headInfoAll", listAllNumberHigh());
			//设置系统参数
			setSystemInfo(dataAll);
		}
		return dataAll;
	}
	
	
	
	/**
	 * 统计所有的数量
	 */
	public MesAgeDevNumber   listAllNumber(Integer start,Integer end) {
		LocalDateTime sTime = LocalDateTime.now();
		MesAgeDevNumber number = new MesAgeDevNumber();
		
		//貨架總數
		String stockNumSq = "SELECT  "+
				"	count(TO_NUMBER(scode))  "+
				"FROM  ( "+
				"	SELECT "+
				"		 "+
				"		SUBSTR(STOCK_CODE,0,2) AS scode   "+
				"	FROM "+
				"		MES_AGE_DEV_BIND_INFO madbi "+
				"	WHERE "+
				"		DEV_TYPE = 'N' "+
				")	 "+
				"WHERE scode  BETWEEN  "+start+"  AND "+end+" ";
		long stockNum = sqlQueryUtils.polyMericSql(stockNumSq, new Object[] {});
		number.setStockNum(new Long(stockNum).intValue());
		StreamaxUtils.caclulateTime(sTime, "老化统货架总数量耗时", logger);
		
		//貨架閒置數
		String defSql =  "SELECT  "+
								"	count(TO_NUMBER(scode))  "+
								"FROM  ( "+
								"	SELECT "+
								"		 "+
								"		SUBSTR(STOCK_CODE,0,2) AS scode   "+
								"	FROM "+
								"		MES_AGE_DEV_BIND_INFO madbi "+
								"	WHERE "+
								"		 mo_id is null and  DEV_TYPE = 'N' "+
								")	 "+
								"WHERE scode  BETWEEN  "+start+"  AND "+end+" ";
		long defNum = sqlQueryUtils.polyMericSql(defSql, new Object[] {});
		number.setDef(new Long(defNum).intValue());//闲置新增1
		StreamaxUtils.caclulateTime(sTime, "老化统计闲置数量耗时", logger);
		
		//客供完成的總數量
		String custFishSql = "SELECT "+
				"	count(1) "+
				"FROM "+
				"	MES_AGE_DEV_INFO info "+
				"INNER JOIN ( "+
				"	SELECT  "+
				"		TO_NUMBER(scode),LB_ID,dev_type  "+
				"	FROM  ( "+
				"		SELECT "+
				"			LB_ID, "+
				"			dev_type, "+
				"			SUBSTR(STOCK_CODE,0,2) AS scode   "+
				"		FROM "+
				"			MES_AGE_DEV_BIND_INFO madbi "+
				"		WHERE "+
				"			DEV_TYPE = 'N' and madbi.lb_id is not null "+
				"	)	 "+
				"	WHERE scode  BETWEEN  "+start+"  AND "+end+" "+
				") bd ON "+
				"	bd.lb_id = info.lb_id "+
				"WHERE "+
				"	info.VALID_FLAG = '1' "+
				"	AND bd.dev_type = 'N' "+
				"	AND info.plat_type = '2' "+
				"	AND ROUND(TO_NUMBER(SYSDATE-info.DIS_CONN_TIME )* 24 * 60 )>15 ";
		long custNum = sqlQueryUtils.polyMericSql(custFishSql, new Object[] {});
		number.setCusfinish(new Long(custNum).intValue());
		StreamaxUtils.caclulateTime(sTime, "老化统计客供完成数量耗时", logger);
		
		
		//自主老化完的總數量
		String finishSql = "SELECT "+
				"	count(1) "+
				"FROM "+
				"	MES_AGE_DEV_INFO info "+
				"INNER JOIN ( "+
				"	SELECT  "+
				"		TO_NUMBER(scode),LB_ID,dev_type  "+
				"	FROM  ( "+
				"		SELECT "+
				"			LB_ID, "+
				"			dev_type, "+
				"			SUBSTR(STOCK_CODE,0,2) AS scode   "+
				"		FROM "+
				"			MES_AGE_DEV_BIND_INFO madbi "+
				"		WHERE "+
				"			DEV_TYPE = 'N' and madbi.lb_id is not null "+
				"	)	 "+
				"	WHERE scode  BETWEEN  "+start+"  AND "+end+" "+
				") bd ON "+
				"	bd.lb_id = info.lb_id "+
				"WHERE "+
				"	info.VALID_FLAG = '1' "+
				"	AND bd.dev_type = 'N' "+
				"	AND info.plat_type = '1' "+
				"	AND info.IS_OVER = '1' ";
		long finishNum = sqlQueryUtils.polyMericSql(finishSql, new Object[] {});
		number.setFinish(new Long(finishNum).intValue());
		StreamaxUtils.caclulateTime(sTime, "老化统计自主老化连接数量耗时", logger);
		
		
		//正常連接的數量
		String normalSql = "SELECT "+
				"	count(1) "+
				"FROM "+
				"	MES_AGE_DEV_INFO info "+
				"INNER JOIN ( "+
				"	SELECT  "+
				"		TO_NUMBER(scode),LB_ID,dev_type  "+
				"	FROM  ( "+
				"		SELECT "+
				"			LB_ID, "+
				"			dev_type, "+
				"			SUBSTR(STOCK_CODE,0,2) AS scode   "+
				"		FROM "+
				"			MES_AGE_DEV_BIND_INFO madbi "+
				"		WHERE "+
				"			DEV_TYPE = 'N' and madbi.lb_id is not null "+
				"	)	 "+
				"	WHERE scode  BETWEEN  "+start+"  AND "+end+" "+
				") bd ON "+
				"	bd.lb_id = info.lb_id "+
				"WHERE "+
				"	info.VALID_FLAG = '1' "+
				"	AND bd.dev_type = 'N' "+
				"	AND info.plat_type = '1' "+
				"	AND info.IS_OVER != '1' "+
				"	AND conn_status = '1' ";
		long normalNum = sqlQueryUtils.polyMericSql(normalSql, new Object[] {});
		number.setNormal(new Long(normalNum).intValue());
		StreamaxUtils.caclulateTime(sTime, "老化统计正常连接数量耗时", logger);
		
		//斷開的數量
		String disConn1 ="SELECT "+
				"	count(1) "+
				"FROM "+
				"	MES_AGE_DEV_INFO info "+
				"INNER JOIN ( "+
				"	SELECT  "+
				"		TO_NUMBER(scode),LB_ID,dev_type  "+
				"	FROM  ( "+
				"		SELECT "+
				"			LB_ID, "+
				"			dev_type, "+
				"			SUBSTR(STOCK_CODE,0,2) AS scode   "+
				"		FROM "+
				"			MES_AGE_DEV_BIND_INFO madbi "+
				"		WHERE "+
				"			DEV_TYPE = 'N' and madbi.lb_id is not null "+
				"	)	 "+
				"	WHERE scode  BETWEEN  "+start+"  AND "+end+" "+
				") bd ON "+
				"	bd.lb_id = info.lb_id "+
				"WHERE "+
				"	info.VALID_FLAG = '1' "+
				"	AND bd.dev_type = 'N' "+
				"	AND info.plat_type = '1' "+
				"	AND info.IS_OVER != '1' "+
				"	AND conn_status = '0' ";
		long disNum1 = sqlQueryUtils.polyMericSql(disConn1, new Object[] {});
		StreamaxUtils.caclulateTime(sTime, "老化统计断开连接数量1耗时", logger);
		
		String disConn2 = "SELECT "+
				"	count(1) "+
				"FROM "+
				"	( "+
				"	SELECT  "+
				"		TO_NUMBER(scode),LB_ID,dev_type  "+
				"	FROM  ( "+
				"		SELECT "+
				"			LB_ID, "+
				"			dev_type, "+
				"			SUBSTR(STOCK_CODE,0,2) AS scode   "+
				"		FROM "+
				"			MES_AGE_DEV_BIND_INFO madbi "+
				"		WHERE "+
				"			DEV_TYPE = 'N' and madbi.lb_id is not null "+
				"	)	 "+
				"	WHERE scode  BETWEEN  "+start+"  AND "+end+" "+
				"	 "+
				"	 "+
				"	) bd "+
				"WHERE "+
				"	bd.lb_id IS NOT NULL "+
				"	AND bd.dev_type = 'N' "+
				"	AND bd.lb_id NOT IN ( "+
				"	SELECT "+
				"		lb_id "+
				"	FROM "+
				"		MES_AGE_DEV_INFO "+
				"	WHERE "+
				"		VALID_FLAG = '1' "+
				"		AND dev_type = 'N') ";
		long disNum2 = sqlQueryUtils.polyMericSql(disConn2, new Object[] {});
		
		
		long alldis = disNum1+disNum2;
		number.setUnconn(new Long(alldis).intValue());
		
		StreamaxUtils.caclulateTime(sTime, "老化统计断开连接数量2耗时", logger);
		
		//查询总异常数量
		String errrorSql = "SELECT "+
				"	count(1) "+
				"FROM "+
				"	MES_AGE_DEV_INFO info "+
				"INNER JOIN ( "+
				"	SELECT  "+
				"		TO_NUMBER(scode),LB_ID,dev_type  "+
				"	FROM  ( "+
				"		SELECT "+
				"			LB_ID, "+
				"			dev_type, "+
				"			SUBSTR(STOCK_CODE,0,2) AS scode   "+
				"		FROM "+
				"			MES_AGE_DEV_BIND_INFO madbi "+
				"		WHERE "+
				"			DEV_TYPE = 'N' and madbi.lb_id is not null "+
				"	)	 "+
				"	WHERE scode  BETWEEN  "+start+"  AND "+end+" "+
				")  bd ON "+
				"	bd.lb_id = info.lb_id "+
				"WHERE "+
				"	info.VALID_FLAG = '1' "+
				"	AND bd.dev_type = 'N' "+
				"	AND info.plat_type = '1' "+
				"	AND info.IS_PASS = 'N' "+
				"	AND info.HAS_ERROR = '1' "+
				"	AND conn_status = '1' ";
		long errorNum = sqlQueryUtils.polyMericSql(errrorSql, new Object[] {});
		number.setError(new Long(errorNum).intValue());
		
		StreamaxUtils.caclulateTime(sTime, "统计所有数量耗时"+errrorSql, logger);
		return number;
	}
	
	
	/**
	 * 统计所有的数量
	 */
	public MesAgeDevNumber   listAllNumberHigh() {
		LocalDateTime sTime = LocalDateTime.now();
		MesAgeDevNumber number = new MesAgeDevNumber();
		
		//貨架總數
		String stockNumSq = "select count(1) from MES_AGE_DEV_BIND_INFO where dev_type='Y' ";
		long stockNum = sqlQueryUtils.polyMericSql(stockNumSq, new Object[] {});
		number.setStockNum(new Long(stockNum).intValue());
		
		StreamaxUtils.caclulateTime(sTime, "高温老化统计货架总数耗时", logger);
		
		
		//貨架閒置數
		String defSql = "select count(1) from MES_AGE_DEV_BIND_INFO where mo_id is null and dev_type='Y' ";
		long defNum = sqlQueryUtils.polyMericSql(defSql, new Object[] {});
		number.setDef(new Long(defNum).intValue());//闲置新增1
		
		StreamaxUtils.caclulateTime(sTime, "高温老化统计闲置数耗时", logger);
		
		
		//客供完成的總數量
		String custFishSql = "	SELECT count(1)  FROM MES_AGE_DEV_INFO info INNER  JOIN MES_AGE_DEV_BIND_INFO bd ON  bd.lb_id = info.lb_id\r\n" + 
				"	WHERE info.VALID_FLAG ='1' AND info.dev_type = 'Y' AND info.plat_type = '2' AND bd.LB_ID is not null AND ROUND(TO_NUMBER(SYSDATE-info.DIS_CONN_TIME )*24*60 )>15 ";
		long custNum = sqlQueryUtils.polyMericSql(custFishSql, new Object[] {});
		number.setCusfinish(new Long(custNum).intValue());
		
		StreamaxUtils.caclulateTime(sTime, "高温老化统计客供完成数耗时", logger);
		
		
		//自主老化完的總數量
		String finishSql = " SELECT count(1) FROM MES_AGE_DEV_INFO info INNER  JOIN MES_AGE_DEV_BIND_INFO bd ON  bd.lb_id = info.lb_id\r\n" + 
				"	WHERE info.VALID_FLAG ='1' AND info.dev_type = 'Y' AND info.plat_type = '1' AND bd.LB_ID is not null AND info.IS_OVER  = '1' ";
		long finishNum = sqlQueryUtils.polyMericSql(finishSql, new Object[] {});
		number.setFinish(new Long(finishNum).intValue());
		
		StreamaxUtils.caclulateTime(sTime, "高温老化统计自主老化完成数耗时", logger);
		
		
		//正常連接的數量
		String normalSql = " SELECT count(1) FROM MES_AGE_DEV_INFO info INNER  JOIN MES_AGE_DEV_BIND_INFO bd ON  bd.lb_id = info.lb_id\r\n" + 
				"	WHERE info.VALID_FLAG ='1' AND bd.LB_ID is not null AND info.dev_type = 'Y' AND info.plat_type = '1' AND info.IS_OVER  != '1' AND conn_status = '1'";
		long normalNum = sqlQueryUtils.polyMericSql(normalSql, new Object[] {});
		number.setNormal(new Long(normalNum).intValue());
		
		StreamaxUtils.caclulateTime(sTime, "高温老化统计连接数量耗时", logger);
		
		
		//斷開的數量
		String disConn1 = "SELECT count(1) FROM MES_AGE_DEV_INFO info INNER  JOIN MES_AGE_DEV_BIND_INFO bd ON  bd.lb_id = info.lb_id\r\n" + 
				"	WHERE info.VALID_FLAG ='1' AND bd.LB_ID is not null AND info.dev_type = 'Y' AND info.plat_type = '1' AND info.IS_OVER  != '1' AND conn_status = '0'";
		long disNum1 = sqlQueryUtils.polyMericSql(disConn1, new Object[] {});
		
		StreamaxUtils.caclulateTime(sTime, "高温老化统计断开数量耗时：", logger);
		
		String disConn2 = "SELECT count(1)  from MES_AGE_DEV_BIND_INFO bd WHERE  bd.lb_id IS NOT NULL and bd.dev_type='Y'  AND bd.lb_id NOT in  (SELECT lb_id FROM MES_AGE_DEV_INFO WHERE VALID_FLAG ='1' and dev_type='Y' )";
		long disNum2 = sqlQueryUtils.polyMericSql(disConn2, new Object[] {});
		
		StreamaxUtils.caclulateTime(sTime, "高温老化统计断开数量2耗时：", logger);
		
		long alldis = disNum1+disNum2;
		number.setUnconn(new Long(alldis).intValue());
		
		//查询总异常数量
		String errrorSql = "SELECT count(1)  FROM MES_AGE_DEV_INFO info INNER  JOIN MES_AGE_DEV_BIND_INFO bd ON  bd.lb_id = info.lb_id\r\n" + 
				"	WHERE info.VALID_FLAG ='1' AND bd.LB_ID is not null AND info.dev_type = 'Y' AND info.plat_type = '1' AND info.IS_PASS  = 'N' AND info.HAS_ERROR  = '1' AND conn_status = '1'";
		long errorNum = sqlQueryUtils.polyMericSql(errrorSql, new Object[] {});
		number.setError(new Long(errorNum).intValue());
		
		StreamaxUtils.caclulateTime(sTime, "高温老化统计异常数量耗时", logger);
		return number;
	}
	
	/**
	 * 根据平台类型统计看板数量
	 * @param ageDevInfoOne
	 * @param mesAgeDevBindInfoForm
	 * @param number
	 */
	public void countNumberByType(MesAgeDevInfoForm ageDevInfoOne,MesAgeDevBindInfoForm mesAgeDevBindInfoForm,MesAgeDevNumber number,int type) {
		//1.客供平台:a.是否过站。b.是否连接。c.是否异常。
		//1.平台版本:a.是否过站。c.是否异常。
		if (type==9) {
			// 设置logo和视频验证
			if (ageDevInfoOne.getLogoCount() > 3) {
				mesAgeDevBindInfoForm.setShowType("logoError");
			}
			if (Integer.parseInt(ageDevInfoOne.getChkVideoResult()) > 5) {
				mesAgeDevBindInfoForm.setShowType("videoError");
			}
		} else {
			/**
			 * 计算数量
			 */
			if (ageDevInfoOne.getIsOver()== StreamaxConstants.IsOver.UP_OK) {//升级完成
				if(type==2) {//客供
					number.setCusfinish(number.getCusfinish()+1);
					mesAgeDevBindInfoForm.setShowType("cusfinish");
				}else {
					number.setFinish(number.getFinish()+1);
					mesAgeDevBindInfoForm.setShowType("finish");
				}
			} else {//未完成
				if (ageDevInfoOne.getConnStatus() == 0) {// 断开
					number.setUnconn(number.getUnconn()+1);
					mesAgeDevBindInfoForm.setShowType("unconn");
				} else if (ageDevInfoOne.getConnStatus() == 1) {// 连接中
					number.setNormal(number.getNormal()+1);
					mesAgeDevBindInfoForm.setShowType("normal");
					if("1".equals(ageDevInfoOne.getHasError())) {
						number.setError(number.getError()+1);
						mesAgeDevBindInfoForm.setShowType("error");
					}
					//设置最新异常的日志
					//setNewErrorLog(ageDevInfoOne, mesAgeDevBindInfoForm, number, type);
				}
			}
		}
		
		/**完善基础数据*/
		complateData(ageDevInfoOne, mesAgeDevBindInfoForm,type) ;
	}
	
	/**
	 * 完善一些计算的数据
	 * @param ageDevInfoOne
	 * @param mesAgeDevBindInfoForm
	 * @param type
	 */
	public void  complateData(MesAgeDevInfoForm ageDevInfoOne,MesAgeDevBindInfoForm mesAgeDevBindInfoForm,int type) {
		// 如果是客制版本，老化时长=（当前时间-升级完成时间），计算老化时长
		if (2 == type) {
			if (ageDevInfoOne.getDisConnTime()== null) {
				ageDevInfoOne.setUpgradeMin(new BigDecimal(0));
			} else {
				ageDevInfoOne.setUpgradeMin(new BigDecimal(N95Utils.calcTime(ageDevInfoOne.getDisConnTime(), new Date(), "m")));
			}
			//计算剩余时长
			if(StringUtils.isEmpty(ageDevInfoOne.getAgeTime()))ageDevInfoOne.setAgeTime(new BigDecimal(0));
			long min = 0;
			if(ageDevInfoOne.getDisConnTime()!=null) {
				 min = N95Utils.calcTime(ageDevInfoOne.getDisConnTime(), new Date(), "m");
			}
			long totalMin = ageDevInfoOne.getAgeTime().multiply(new BigDecimal(60)).longValue();
			//老化时长大于总时长
			if(min >= totalMin) {
				ageDevInfoOne.setDiffMin(BigDecimal.ZERO);
			}else {
				ageDevInfoOne.setDiffMin(new BigDecimal(totalMin).subtract(new BigDecimal(min)));
			}
			if(ageDevInfoOne.getDisConnTime()!=null) {
				//开始时间
				ageDevInfoOne.setStartTime(new SimpleDateFormat("MM/dd HH:mm").format(ageDevInfoOne.getDisConnTime()));
			}
		}else {
			// 等待时长=当前时间-设备最后一次断开连接时间
			if (ageDevInfoOne.getDisConnTime() != null) {
				mesAgeDevBindInfoForm.setWaitMin(String.valueOf(N95Utils.calcTime(ageDevInfoOne.getDisConnTime(), new Date(), "m")));
			}
			
			if(StringUtils.isEmpty(ageDevInfoOne.getAgeTime()))ageDevInfoOne.setAgeTime(new BigDecimal(0));
			//剩余时间 = 老化时间-已经老化的时间
			long totalMin = ageDevInfoOne.getAgeTime().multiply(new BigDecimal(60)).longValue();
			BigDecimal upGradeMinBigDecimal = ageDevInfoOne.getUpgradeMin();
			ageDevInfoOne.setDiffMin(new BigDecimal(totalMin).subtract(upGradeMinBigDecimal));
			//开始时间
			ageDevInfoOne.setStartTime(new SimpleDateFormat("MM/dd HH:mm").format(ageDevInfoOne.getCreatedDate()));
			if(1==ageDevInfoOne.getIsOver()) {//升级完成的
				String versionString = ageDevInfoOne.getFtpSoft1();
				
				String reg = "[A-Z]{1}\\d{6}";
				Pattern p  = Pattern.compile(reg);
				Matcher m = p.matcher(versionString);
				while(m.find()) {
					versionString = m.group(0);
					break;
				}
				ageDevInfoOne.setNowVersion(versionString);
//				try {
//					//_在倒数第二次出现的位置
//					int last2 = StringUtils.lastOrdinalIndexOf(versionString, "_", 2);
//					if(last2==-1) {
//						last2 = StringUtils.lastOrdinalIndexOf(versionString, "-", 2);
//					}
//					//在倒数第三次出现的位置
//					int last3 = StringUtils.lastOrdinalIndexOf(versionString, "_", 3);
//					if(last3==-1) {
//						last3 = StringUtils.lastOrdinalIndexOf(versionString, "-", 3);
//					}
//					if(last2!=-1&&(last3+1<last2)) {
//						String nowVersion = versionString.substring(last3+1,last2);
//						ageDevInfoOne.setNowVersion(nowVersion);
//					}else {
//						ageDevInfoOne.setNowVersion(versionString);
//					}
//				}catch (Exception e) {
//					e.printStackTrace();
//					ageDevInfoOne.setNowVersion(versionString);
//				}
			}
		}
		mesAgeDevBindInfoForm.setMesAgeDevInfoDto(ageDevInfoOne);
	}
	
	public static void main(String[] args) {
		String versionString = "K-RM-D550-T08-1.0MR-210617-C02.0380";
		
		String reg = "[A-Z]{1}\\d{6}";
		Pattern p  = Pattern.compile(reg);
		Matcher m = p.matcher(versionString);
		while(m.find()) {
			versionString = m.group(0);
			break;
		}
		
		System.out.println(versionString);
	}
	
	
	
	/**
	 * 正常连接情况下，获取最新日志信息
	 * @param ageDevInfoOne
	 * @param mesAgeDevBindInfoForm
	 * @param number
	 * @param type
	 */
	public void setNewErrorLog(MesAgeDevInfoForm ageDevInfoOne,MesAgeDevBindInfoForm mesAgeDevBindInfoForm,MesAgeDevNumber number,int type) {
		// 获取最新异常日志信息
		MesAgeDevLogInfoDto logParams = new MesAgeDevLogInfoDto();
		logParams.setAgeDevInfoId(ageDevInfoOne.getAgeDevInfoId());
		List<MesAgeDevLogInfoDto> logs = listAgeDevInfoLogAll(logParams);
		if (logs != null && logs.size() > 0) {
			MesAgeDevLogInfoDto logInfoDto = logs.get(0);
			if("发生异常".equals(logInfoDto.getEventName())||"其它异常".equals(logInfoDto.getEventName())) {
				if(type==StreamaxConstants.PlatType.PLAT_TYPE_CUSTOMER&&ageDevInfoOne.getConnStatus()==StreamaxConstants.ConnStatus.DISCONNECT) {//客供的不需要看到错误日志
					//跳过
				}else {
					number.setError(number.getError()+1);
					mesAgeDevBindInfoForm.setShowType("error");
					mesAgeDevBindInfoForm.setMesAgeDevLogInfoDto(logInfoDto);
				}
			}
		}
	}
	
	/**
	 * 设置系统变量
	 * @param dataAll
	 */
	public void setSystemInfo(Map<String, Object> dataAll) {
		dataAll.put("time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));//设置当前服务器时间
		dataAll.put("ip", N95Utils.getIpAddr());//设置服务器ip
		// 获取端口信息
		String socketPort = configService.getConfigValue(BsConstants.SYSTEM_IMS_MES_CODE, "FTP_INFO_PORT", "102",
				null, String.class);
		;// 获取服务端口
		Integer portInteger = 5556;
		if ("null".equals(socketPort) || StringUtils.isBlank(socketPort)) {
		} else {
			try {
				portInteger = Integer.parseInt(socketPort);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		dataAll.put("port", portInteger);//设置设备连接的服务器端口
	}
	
	/**
	 * 获取设备的详细信息
	 * @param params
	 * @return
	 */
	public List<MesAgeDevInfoForm> listAgeDevInfoAll(MesAgeDevInfoDto params) {
//		该注释需要取消
		if(StringUtils.isBlank(params.getAgeDevInfoId())) {
			return listDevInfoByMo(params);
		}
		//mg.ftp_path1,mg.ftp_soft1,mg.SOFT_VERSION,ex.age_time,mg.plat_type
		// 根据标签id和工单Id查找设备信息
		String sqlAgeDev = " SELECT ad.*  FROM  mes_age_dev_info ad "
				+ "WHERE ad.mo_id = ? and ad.age_dev_info_id=? ";
		List<MesAgeDevInfoForm> ageDevInfo = sqlQueryUtils.queryAll(sqlAgeDev,
				new Object[] { params.getMoId(), params.getAgeDevInfoId() }, MesAgeDevInfoForm.class);
		for (MesAgeDevInfoForm mesAgeDevInfoForm : ageDevInfo) {
			if (StringUtils.isNotBlank(mesAgeDevInfoForm.getStorageTotal())) {
				mesAgeDevInfoForm.setStorageTotal(N95Utils.countCapacityGB(mesAgeDevInfoForm.getStorageTotal(), "0"));
			} else {
				mesAgeDevInfoForm.setStorageTotal("0");
			}
			if (StringUtils.isNotBlank(mesAgeDevInfoForm.getStorageLast())) {
				mesAgeDevInfoForm.setStorageLast(N95Utils.countCapacityGB(mesAgeDevInfoForm.getStorageLast(), "0"));
			} else {
				mesAgeDevInfoForm.setStorageLast("0");
			}
			Integer platType = ageDevInfo.get(0).getPlatType();
			if(platType==null)platType=1;
			// 如果是客制版本，老化时长为当前时间-升级完成时间
			if (2 == platType) {
				if (ageDevInfo.get(0).getDisConnTime()== null) {
					ageDevInfo.get(0).setUpgradeMin(new BigDecimal(0));
				} else {
					ageDevInfo.get(0).setUpgradeMin(
							new BigDecimal(N95Utils.calcTime(ageDevInfo.get(0).getDisConnTime(), new Date(), "m")));
				}
			}
//			if (mesAgeDevInfoForm.getLogoCount() > 3) {
//				logoError = true;
//			}
//			if (Integer.parseInt(mesAgeDevInfoForm.getChkVideoResult()) > 5) {
//				videoError = true;
//			}
		}
		return ageDevInfo;
	}

	
	public List<MesAgeDevInfoForm>   listDevInfoByMo(MesAgeDevInfoDto params){
		String sqlAgeDev =" SELECT "+
				"	bm.mtrl_code AS prod_code, "+
				"	mo.mo_code,mo.prod_id "+
				" FROM "+
				"	bs_mo mo "+
				"LEFT JOIN BS_MTRL bm ON "+
				"	bm.MTRL_ID = mo.PROD_ID "+
				"WHERE "+
				"	mo.mo_id = ? ";
		List<MesAgeDevInfoForm> ageDevInfo = sqlQueryUtils.queryAll(sqlAgeDev,
				new Object[] { params.getMoId() }, MesAgeDevInfoForm.class);
		for (MesAgeDevInfoForm mesAgeDevInfoForm : ageDevInfo) {
			
				mesAgeDevInfoForm.setStorageTotal("0");
				mesAgeDevInfoForm.setStorageLast("0");
			// 如果是客制版本，老化时长为当前时间-升级完成时间
				mesAgeDevInfoForm.setLbId(params.getLbId());
				mesAgeDevInfoForm.setUpgradeMin(BigDecimal.ZERO);
				mesAgeDevInfoForm.setConnCount(0);
		}
		return ageDevInfo;
	}
	
	
	
	public List<MesAgeDevLogInfoDto> listAgeDevInfoLogAll(MesAgeDevLogInfoDto params) {
		String sqlString = "SELECT * FROM mes_age_dev_log_info  WHERE age_dev_info_id= ?  ORDER BY CREATED_DATE_NEW desc ";
		List<MesAgeDevLogInfoDto> ageDevInfo = sqlQueryUtils.queryAll(sqlString, new Object[] { params.getAgeDevInfoId()},
				MesAgeDevLogInfoDto.class);
		return ageDevInfo;
	}

	/**
	 * 大于五次的异常连接，重置次数可以重新连接
	 * @param params
	 * @return
	 */
	public ResultMap relink(MesAgeDevInfoDto params) {
		int result = 0;
		try {
			MesAgeDevInfoDto mesAgeDevInfo = new MesAgeDevInfoDto();
			mesAgeDevInfo.setAgeDevInfoId(params.getAgeDevInfoId());
			List<MesAgeDevInfoDto> lists = mesAgeDevInfoService.select(mesAgeDevInfo);
			if (lists != null && lists.size() > 0) {
				if (lists.get(0).getConnCount() > 5) {// 大于五次可以設置重新連接
					// 修改升级完成扫描状态
					MesAgeDevInfoDto record = new MesAgeDevInfoDto();
					record.setConnCount(0);
					record.setAgeDevInfoId(params.getAgeDevInfoId());
					result = mesAgeDevInfoService.updateByPrimaryKeyNotNull(record);
					if (result > 0)
						return ResultMap.pass("操作成功,将进行重新连接...");
				} else {
					return ResultMap.fail("非重连过六次的设备无需手动连接!");
				}
			} else {
				return ResultMap.fail("未查询到设备信息记录");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ResultMap.fail();
		}
		return ResultMap.fail("操作失败");
	}

	/**
	 * 重新升级
	 * @param params
	 * @return
	 */
	public ResultMap reUpgrade(MesAgeDevInfoDto params) {
		int result = 0;
		try {
			MesAgeDevInfoDto mesAgeDevInfo = new MesAgeDevInfoDto();
			mesAgeDevInfo.setAgeDevInfoId(params.getAgeDevInfoId());
			List<MesAgeDevInfoDto> lists = mesAgeDevInfoService.select(mesAgeDevInfo);
			if (lists != null && lists.size() > 0) {
				if (lists.get(0).getConnStatus() == StreamaxConstants.ConnStatus.CONNECT) {// 正在连接可以重新升级
					if (lists.get(0).getIsOver() == StreamaxConstants.IsOver.UP_HAS_SEND||lists.get(0).getIsOver() == StreamaxConstants.IsOver.UP_HAS_BACK_) {
						return ResultMap.fail("当前设备已经正在升级中...");
					}
					if ("Y".equals(lists.get(0).getIsPass())) {
						return ResultMap.fail("当前设备已经过站，无需重新升级！");
					}
					// 重新扫描硬盘及重新升级
					MesAgeDevInfoDto record = new MesAgeDevInfoDto();
					record.setIsOver(StreamaxConstants.IsOver.NO_UP);
					record.setErrorCount(0);//重置过站错误次数
					record.setIsUpThround("N");//重置是否有上传100进度
					record.setUpCount(0);//重置升级未响应次数
					Condition condition = new Condition(MesAgeDevInfoDto.class);
					Criteria criteria = condition.createCriteria();
					criteria.andEqualTo("ageDevInfoId", params.getAgeDevInfoId());
					result = mesAgeDevInfoService.updateByConditionNotNull(record, condition);
					if (result > 0)
						return ResultMap.pass("操作成功,将进行重新升级...");
				} else {
					return ResultMap.fail("当前设备非稳定连接中状态，不能操作");
				}
			} else {
				return ResultMap.fail("未查询到设备信息记录");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ResultMap.fail();
		}
		return ResultMap.fail("升级失败");
	}

	/**
	 * 站点释放
	 * 
	 * @param params
	 * @return
	 */
	public ResultMap updateByStockCode(MesAgeDevBindInfoDto params) {
		int result = 0;
		try {
			MesAgeDevBindInfoDto mesAgeDevBindInfo = new MesAgeDevBindInfoDto();
			mesAgeDevBindInfo.setStockCode(params.getStockCode());
			List<MesAgeDevBindInfoDto> lists = mesAgeDevBindInfoService.select(mesAgeDevBindInfo);
			if (lists != null && lists.size() > 0) {
				MesAgeDevBindInfoDto bindInfo = lists.get(0);
				if (StringUtils.isNotBlank(bindInfo.getMoId())) {// 更新设备数据，清空货架绑定信息
					MesAgeDevInfoDto mesAgeDevInfo = new MesAgeDevInfoDto();
					mesAgeDevInfo.setLbId(bindInfo.getLbId());
					mesAgeDevInfo.setMoId(bindInfo.getMoId());
					mesAgeDevInfo.setValidFlag(StreamaxConstants.VALID_FLAG_OK);
					List<MesAgeDevInfoDto> ageDevInfo = mesAgeDevInfoService.select(mesAgeDevInfo);
					if (ageDevInfo != null && ageDevInfo.size() > 0) {
						// 更新设备状态
						MesAgeDevInfoDto upMesAgeDevInfo = new MesAgeDevInfoDto();
						upMesAgeDevInfo.setAgeDevInfoId(ageDevInfo.get(0).getAgeDevInfoId());
						upMesAgeDevInfo.setConnStatus(StreamaxConstants.ConnStatus.DISCONNECT);// 更新为断开
						result = mesAgeDevInfoService.updateByPrimaryKeyNotNull(upMesAgeDevInfo);
					} 
					//更新货架信息
					// 删除绑定信息
					bindInfo.setMoId(null);
					bindInfo.setLbId(null);
					bindInfo.setNewLbId(null);
					bindInfo.setBindDate(null);
					bindInfo.setIsValid(StreamaxConstants.VALID_FLAG_NG);
					bindInfo.setErrorMessage("");
					result = mesAgeDevBindInfoService.updateByPrimaryKey(bindInfo);
					if (result > 0)
					return ResultMap.pass("站点释放成功");
				} else {
					if(StringUtils.isNotBlank(lists.get(0).getLbId())){
						// 删除绑定信息
						bindInfo.setMoId(null);
						bindInfo.setLbId(null);
						bindInfo.setNewLbId(null);
						bindInfo.setBindDate(null);
						bindInfo.setIsValid(StreamaxConstants.VALID_FLAG_NG);
						bindInfo.setErrorMessage("");
						result = mesAgeDevBindInfoService.updateByPrimaryKey(bindInfo);
						if (result > 0)	return ResultMap.pass("站点释放成功");
					}else {//设备没有
						//直接清空货架记录
						return ResultMap.fail("货架未绑定标签信息无需释放!");
					}
				}
			}
			return ResultMap.fail("站点释放失败");
		} catch (Exception e) {
			e.printStackTrace();
			return ResultMap.fail();
		}
	}
	
	
	/**
	 * 托盘释放
	 * 
	 * @param params
	 * @return
	 */
	public ResultMap updateByStockCodeHigh(MesAgeDevBindInfoDto params) {
		try {
			String stockCode = params.getStockCode();
			String prfix = stockCode.substring(0,stockCode.lastIndexOf("_"));
			
			
			String s1 = prfix+"_01";
			String s2 = prfix+"_02";
			//查找高温老化未解绑过的数据
			String sqlAgeDev = " SELECT  *   FROM MES_AGE_DEV_BIND_INFO  WHERE  DEV_TYPE ='Y'  AND STOCK_CODE IN ('"+s1+"','"+s2+"') and lb_id is not null";
			List<MesAgeDevBindInfoDto> bindInfos = sqlQueryUtils.queryAll(sqlAgeDev, new Object[] {},
					MesAgeDevBindInfoDto.class);
			int count = 0;
			if (bindInfos != null && (bindInfos.size() == 2||bindInfos.size()==1)) {
				for (MesAgeDevBindInfoDto mesAgeDevBindInfoDto : bindInfos) {
					if(mesAgeDevBindInfoDto.getSignalStat()==StreamaxConstants.SINGAL_STAT.SINGAL_STAT_0) { //已经发送过
						mesAgeDevBindInfoDto.setSignalStat(StreamaxConstants.SINGAL_STAT.SINGAL_STAT_1);
						int res = mesAgeDevBindInfoService.updateByPrimaryKey(mesAgeDevBindInfoDto);
						count+=res;
					}else{ //已经发送过
						return ResultMap.fail("托盘正在排出处理中，请等待操作完成！");
					}
				}
				if(count==2) {
					return ResultMap.pass("操作成功,请等待机器取出托盘！");
				}else {
					return ResultMap.pass("操作失败！");
				}
			}else {
				return ResultMap.fail("托盘释放失败，未找到托盘1【"+s1+"】和托盘2【"+s2+"】一对记录");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ResultMap.fail();
		}
	}

}
